package cn.xianyang.basic.util;

import cn.xianyang.basic.jwt.JwtUtils;
import cn.xianyang.basic.jwt.LoginResult;
import cn.xianyang.basic.jwt.Payload;
import cn.xianyang.basic.jwt.RsaUtils;
import cn.xianyang.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {
    /**
     * 获取当前登录人
     * @param request
     * @return
     */
    public static Logininfo getCurrentLogininfo(HttpServletRequest request){
        //1 .从请求头中获取U-TOKEN   --JWT 串
        String token = request.getHeader("U-TOKEN");
        //2 .现在需要得到登录人和登录对应的权限，解密JWT串 ....获取共钥
        PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
        //3 .使用公钥去解密JWT串
        Payload<LoginResult> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginResult.class);
        LoginResult loginResult = payload.getUserInfo();
        return loginResult.getLogininfo();

    }
}
